package com.jiangchuang.service.impl;

import com.jiangchuang.service.ICommonService;
import com.jiangchuang.utils.OssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@Service
public class CommonServiceImpl implements ICommonService {

    @Override
    public String uploadImg(String prefix,MultipartFile img) {
        // 生成随机值
        String uuid = UUID.randomUUID().toString().replace("-","");
        int dotIndex = img.getOriginalFilename().lastIndexOf('.');
        String houzhui = img.getOriginalFilename().substring(dotIndex);
        try{
            // 上传原图
            String url = OssUtils.upload(prefix +uuid+houzhui, img);

            // 返回URL
            return url;
        }catch (Exception e){
            throw new RuntimeException("上传图片失败uploadImg："+e.getMessage());
        }
    }


}
